/**
 * 获取本地图
 * @param folder // 文件夹名字 如 /static/images/home
 * @param fileName
 * @param format // 文件类型 如 png jpg
 * @returns {*|string}
 */
import dayjs from 'dayjs';

// #ifdef MP-WEIXIN
export const urlTobase64 = (folder: string, fileName: string, format = 'png') => {
  const img = `/static/image/${folder}/${fileName}.${format}`;
  const imgBase64 = wx.getFileSystemManager().readFileSync(img, 'base64');
  return `data:image/png;base64,${imgBase64}`;
};
// #endif

/**
 * 拨打电话
 * @returns {*|Promise|Promise.<*>}
 * @param phoneNumber
 */

// #ifdef MP-WEIXIN
export const makeCall = (phoneNumber: string) => {
  return new Promise((resolve, reject) => {
    uni.makePhoneCall({
      phoneNumber,
      success: (result) => {
        resolve(result);
      },
      fail: (result) => {
        reject(result);
      }
    });
  });
};

// #endif

interface ToLocationParams {
  address: string;
  longitude: number;
  latitude: number;
  name: string;
}

/**
 * 导航位置
 * @returns {*|Promise|Promise.<*>}
 * @param options
 */

// #ifdef MP-WEIXIN
export const toLocation = (options: ToLocationParams) => {
  return new Promise((resolve, reject) => {
    uni.openLocation({
      name: options.name, // 位置名字
      latitude: options.latitude, // 维度
      longitude: options.longitude, // 经度
      address: options.address, // 地址详细说明
      success: (result) => {
        resolve(result);
      },
      fail: (result) => {
        reject(result);
      }
    });
  });
};
// #endif

// 文件图片一张解析
export function getPicture(photo) {
  const picture = JSON.parse(photo || '[]');
  if (picture.length) {
    return picture[0].path;
  }
  return '';
}

export function calculateAge(idCard) {
  // 从身份证中提取出生日期
  const birthDate = dayjs(idCard.substring(6, 14), { format: 'YYYYMMDD' });

  // 获取当前日期
  const currentDate = dayjs();

  // 计算年龄
  const age = currentDate.diff(birthDate, 'year');

  return age;
}
